﻿using Application.Features.Menus.DTOs;
using Application.Features.Roles.Commands.Add;
using Application.Features.Roles.Commands.Delete;
using Application.Features.Roles.Commands.Update;
using Application.Features.Roles.DTOs;
using Application.Features.Roles.Queries.GetAll;
using Application.Features.Roles.Queries.GetMenusById;
using Application.Features.Roles.Queries.Pagination;
using Common.Models;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;

namespace WebAPI.Controllers;

/// <summary>
/// 角色管理
/// </summary>
[Description("角色管理")]
public class RoleController : ApiControllerBase
{
    /// <summary>
    /// 全部查询
    /// </summary>
    /// <returns></returns>
    [HttpGet("query/all/{userId?}")]
    [Description("全部查询")]
    public async Task<Result<IEnumerable<RoleOutput>>> GetAllRolesAsync(long? userId)
    {
        var data = await Mediator.Send(new GetAllRolesQuery() { UserId = userId });
        return await Result<IEnumerable<RoleOutput>>.SucceedAsync(data);
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <returns></returns>
    [HttpPost("query/pagination")]
    [Description("分页查询")]
    public async Task<Result<PaginatedData<RoleOutput>>> PaginationQueryAsync(RolesWithPaginationQuery query)
    {
        var data = await Mediator.Send(query);
        return await Result<PaginatedData<RoleOutput>>.SucceedAsync(data);
    }

    /// <summary>
    /// 查询权限
    /// </summary>
    /// <returns></returns>
    [HttpGet("{roleId}/query/menu")]
    [Description("查询权限")]
    public async Task<Result<IEnumerable<MenuOutput>>> GetRoleMenusByRoleIdQueryAsync(long roleId)
    {
        var query = await Mediator.Send(new GetRoleMenusByRoleIdQuery() { RoleId = roleId });
        return await Result<IEnumerable<MenuOutput>>.SucceedAsync(query);
    }

    /// <summary>
    /// 创建
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Description("创建")]
    public async Task<Result> AddAsync(AddRoleCommand command)
    {
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改角色
    /// </summary>
    /// <returns></returns>
    [HttpPut("{id}")]
    [Description("修改")]
    public async Task<Result> UpdateAsync(long id, UpdateRoleCommand command)
    {
        command.RoleId = id;
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    /// <param name="ids">删除多个请用英文下的逗号,分开</param>
    /// <returns></returns>
    [HttpDelete("delete/{ids}")]
    [Description("删除")]
    public async Task<Result> DeleteAsync(string ids)
    {
        await Mediator.Send(new DeleteRoleCommand { RoleIds = ids.Split(',').Select(long.Parse).ToList() });
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 分配权限
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    [HttpPut("{roleId}/assign/menu")]
    [Description("分配权限")]
    public async Task<Result> AssignMenuToRoleAsync(AssignMenuToRoleCommand command)
    {
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }

    /// <summary>
    /// 修改状态
    /// </summary>
    /// <returns></returns>
    [HttpPut("{roleId}/status")]
    [Description("修改状态")]
    public async Task<Result> UpdateStatusAsync(long roleId, UpdateRoleStatusCommand command)
    {
        command.RoleId = roleId;
        await Mediator.Send(command);
        return await Result.SucceedAsync();
    }
}